package sdk.pay.sqlite;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

import sdk.pay.model.TimeStatisticsModel;
import sdk.pay.utils.PayLogUtil;

/**
 * Created by Joe on 2017/6/10 0010 11:11.
 * DatabaseControl
 */

public class DatabaseControl {

    public static ArrayList<TimeStatisticsModel> getAllDatas(SQLiteDatabase db) {
        Cursor cursor = null;
        try {
            ArrayList<TimeStatisticsModel> arrayList = new ArrayList<>();
            cursor = db.rawQuery("select * from TBL_TIME_STATISTICS", null);
            while (cursor.moveToNext()) {
                TimeStatisticsModel model = new TimeStatisticsModel();
                model.setId(cursor.getInt(0));
                model.setPlatform(cursor.getString(1));
                model.setArea(cursor.getString(2));
                model.setDeviceType(cursor.getString(3));
                model.setOperator(cursor.getString(4));
                model.setServiceTime(cursor.getString(5));
                model.setServiceInfo(cursor.getString(6));
                model.setTokenTime(cursor.getString(7));
                model.setTokenInfo(cursor.getString(8));
                model.setPaylistTime(cursor.getString(9));
                model.setPaylistInfo(cursor.getString(10));
                model.setPayUrlTime(cursor.getString(11));
                model.setPayUrlInfo(cursor.getString(12));
                model.setPayTime(cursor.getString(13));
                model.setPayInfo(cursor.getString(14));
                model.setCallbackTime(cursor.getString(15));
                model.setCallbackInfo(cursor.getString(16));
                model.setSystemTime(cursor.getString(17));
                model.setRemark(cursor.getString(18));
                model.setOrderNumber(cursor.getString(19));
                model.setTokenUrl(cursor.getString(20));
                model.setUserCode(cursor.getString(21));

                arrayList.add(model);
            }
            return arrayList;
        } catch (Exception e) {
            e.printStackTrace();
            PayLogUtil.logFourth("DatabaseControl sql select all sqlite data error");
            return null;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    public static void updateData(SQLiteDatabase db, TimeStatisticsModel model) {
        try {
            String sqlStr = "UPDATE TBL_TIME_STATISTICS " +
                    "SET CALLBACK_TIME = \'" + model.getCallbackTime() + "\', CALLBACK_INFO = \'" +
                    model.getCallbackInfo() + "\' WHERE SYSTEM_TIME=\'" + model.getSystemTime() + "\'";
            db.execSQL(sqlStr);
        } catch (Exception e) {
            e.printStackTrace();
            PayLogUtil.logFourth("DatabaseControl sql updateData sqlite data error");
        }
    }

    public static void insertData(SQLiteDatabase db, TimeStatisticsModel model) {
        try {
            ContentValues contentValues = new ContentValues();
            contentValues.put("PLATFORM", model.getPlatform());
            contentValues.put("AREA", model.getArea());
            contentValues.put("DEVICE_TYPE", model.getDeviceType());
            contentValues.put("OPERATOR", model.getOperator());
            contentValues.put("SERVICE_TIME", model.getServiceTime());
            contentValues.put("SERVICE_INFO", model.getServiceInfo());
            contentValues.put("TOKEN_TIME", model.getTokenTime());
            contentValues.put("TOKEN_INFO", model.getTokenInfo());
            contentValues.put("PAYLIST_TIME", model.getPaylistTime());
            contentValues.put("PAYLIST_INFO", model.getPaylistInfo());
            contentValues.put("PAYURL_TIME", model.getPayUrlTime());
            contentValues.put("PAYURL_INFO", model.getPayUrlInfo());
            contentValues.put("PAY_TIME", model.getPayTime());
            contentValues.put("PAY_INFO", model.getPayInfo());
            contentValues.put("CALLBACK_TIME", model.getCallbackTime());
            contentValues.put("CALLBACK_INFO", model.getCallbackInfo());
            contentValues.put("SYSTEM_TIME", model.getSystemTime());
            contentValues.put("REMARK", model.getRemark());
            contentValues.put("ORDER_NUMBER", model.getOrderNumber());
            contentValues.put("GET_TOKEN_URL", model.getTokenUrl());
            contentValues.put("USERCODE", model.getUserCode());

            db.insert("TBL_TIME_STATISTICS", null, contentValues);
        } catch (Exception e) {
            e.printStackTrace();
            PayLogUtil.logFourth("DatabaseControl sql insert sqlite data error");
        }
    }

    static void creatAllTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        try {
            db.execSQL("CREATE TABLE " + constraint + "'TBL_TIME_STATISTICS' (" + //
                    "'_id' INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                    "'PLATFORM' TEXT," + // 1: platform
                    "'AREA' TEXT," + // 2: area
                    "'DEVICE_TYPE' TEXT," + // 3: deviceType
                    "'OPERATOR' TEXT," + // 4: operator
                    "'SERVICE_TIME' TEXT," + // 5: serviceTime
                    "'SERVICE_INFO' TEXT," + // 6: serviceInfo
                    "'TOKEN_TIME' TEXT," + // 7: tokenTime
                    "'TOKEN_INFO' TEXT," + // 8: tokenInfo
                    "'PAYLIST_TIME' TEXT," + // 9: paylistTime
                    "'PAYLIST_INFO' TEXT," + // 10: paylistInfo
                    "'PAYURL_TIME' TEXT," + // 11: payTime
                    "'PAYURL_INFO' TEXT," + // 12: payInfo
                    "'PAY_TIME' TEXT," + // 13: payTime
                    "'PAY_INFO' TEXT," + // 14: payInfo
                    "'CALLBACK_TIME' TEXT," + // 15: callbackTime
                    "'CALLBACK_INFO' TEXT," + // 16: callbackInfo
                    "'SYSTEM_TIME' TEXT," + // 17: systemTime
                    "'REMARK' TEXT," + // 18: remark
                    "'ORDER_NUMBER' TEXT," + // 19: orderNumber
                    "'GET_TOKEN_URL' TEXT," +// 20: getTokenUrl
                    "'USERCODE' TEXT);"); // 21: usercode
            // Add Indexes
            db.execSQL("CREATE INDEX " + constraint + "IDX_TBL_TIME_STATISTICS_SYSTEM_TIME ON TBL_TIME_STATISTICS" +
                    " (SYSTEM_TIME);");
        } catch (SQLException e) {
            e.printStackTrace();
            PayLogUtil.logFourth("DatabaseControl sql create table error");
        }
    }

    public static void deleteTable(SQLiteDatabase db) {
        try {
            db.execSQL("DELETE FROM TBL_TIME_STATISTICS");
        } catch (Exception e) {
            e.printStackTrace();
            PayLogUtil.logFourth("DatabaseControl sql deleteTable sqlite error");
        }
    }

}
